﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace NetBay.Core.BusinessEntities.AdvancedSearchCriteria
{
    /// <summary>
    /// Represent an Advanced Search Criteria Item
    /// </summary>
    [DataContract]   
    public class AdvancedSearchCriteriaItem: ReturnObject
    {
        #region Attributs
        /// <summary>
        /// Table name field.
        /// </summary>
        private long _IndexIdentifier;

        /// <summary>
        /// Operator request.
        /// </summary>
        private SqlEqualityOperator _operator;

        /// <summary>
        /// Criteria value.
        /// </summary>        
        private List<string> _criteriaValue;
        
        #endregion
        
        #region Properties
        /// <summary>
        /// Get or Set the Index Identifier
        /// </summary>
        [DataMember]
        public long IndexIdentifier
        {
            get { return _IndexIdentifier; }
            set { _IndexIdentifier = value; }
        }

        /// <summary>
        /// Get or Set operator table field name.
        /// </summary>
        [DataMember]
        public SqlEqualityOperator Operator
        {
            get {return _operator;}
            set {_operator = value;}
        }

        /// Get or Set operator table field name.
        /// </summary>
        [DataMember]
        public List<string> CriteriaValue
        {
            get {return _criteriaValue;}
            set {_criteriaValue = value;}
        }

        #endregion

        #region Contructor
        
        /// <summary>
        /// Initialize AdvancedSearchCriteriaItem
        /// </summary>
        public AdvancedSearchCriteriaItem()
        {
            //TableFieldName = string.Empty;
            Operator = SqlEqualityOperator.None;
            CriteriaValue=new List<string>();
        }

        #endregion

        #region Method

        /// <summary>
        /// Methode AdvancedSearchCriteriaItem's Tostring.
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            object[] args = { IndexIdentifier, Operator, CriteriaValue };
            return string.Format("Object AdvancedSearchCriteriaItem value are: (IndexIdentifier<{0}>) (Operator<{1}>) (CriteriaValue<{2}>)", args);
        }

        /// <summary>
        /// Return the sql operator
        /// </summary>
        /// <returns></returns>
        public string GetSqlOperator()
        {
            return ParseNameOperator(this.Operator);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string GetSqlBetweenValue()
        {
            if (this.Operator == SqlEqualityOperator.Between)
                return string.Format(ParseNameOperator(this.Operator), _criteriaValue[0], _criteriaValue[1]);
            else
                return string.Empty;
        }

        /// <summary>
        /// Method AdvancedSearchCriteriaItem's name operator parse to obtains the operator sql.
        /// </summary>
        /// <returns></returns>
        public static string ParseNameOperator(SqlEqualityOperator p_sNameOperator)
        {
            string l_sReturnOperatorSql="";
            switch (p_sNameOperator)
            {
                case SqlEqualityOperator.None:
                    l_sReturnOperatorSql = "";        
                    break;
                case SqlEqualityOperator.Equal:
                    l_sReturnOperatorSql = "=";
                    break;
                case SqlEqualityOperator.Different:
                    l_sReturnOperatorSql = "!=";
                    break;
                case SqlEqualityOperator.Between:
                    l_sReturnOperatorSql = ">={0} AND {1}<=";
                    break;
                case SqlEqualityOperator.Superior:
                    l_sReturnOperatorSql = ">";
                    break;
                case SqlEqualityOperator.SuperiorOrEqual:
                    l_sReturnOperatorSql = ">=";
                    break;
                case SqlEqualityOperator.Inferior:
                    l_sReturnOperatorSql = "<";
                    break;
                case SqlEqualityOperator.InferiorOrEqual:
                    l_sReturnOperatorSql = "<=";
                    break;
            }
            return l_sReturnOperatorSql;
        }


        /// <summary>
        /// Methode AdvancedSearchCriteriaItem's name operator parse to obtains the operator sql.
        /// </summary>
        /// <returns></returns>
        public static SqlEqualityOperator ParseOperator(string p_sNameOperator)
        {
            switch (p_sNameOperator)
            {
                case "":
                    return SqlEqualityOperator.None;
                case "=":
                    return SqlEqualityOperator.Equal;
                case "!=":
                    return SqlEqualityOperator.Different;
                case ">=;<=":
                    return SqlEqualityOperator.Between;
                case ">":
                    return SqlEqualityOperator.Superior;
                case ">=":
                    return SqlEqualityOperator.SuperiorOrEqual;
                case "<":
                    return SqlEqualityOperator.Inferior; 
                case "<=":
                    return SqlEqualityOperator.InferiorOrEqual;
                default:
                    return SqlEqualityOperator.None;
            }
        }       

        #endregion
    }

}

     
     